package xyz.zhouzhaodong.springbootredisqueue.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;

/**
 * @author zhouzhaodong
 */
@Slf4j
@Service
public class MessageSubscriber {

    public MessageSubscriber(RedisTemplate redisTemplate) {
        RedisConnection redisConnection = redisTemplate.getConnectionFactory().getConnection();
        redisConnection.subscribe((message, bytes) -> {
            // 收到消息的处理逻辑
            log.info("Receive message : " + message);
            // 下面这里需要配置接收的CHANNEL名称
        }, "helloMessage".getBytes(StandardCharsets.UTF_8));

    }

}
